<?php
namespace App\Domain;
use App\Model\Vip as Model_Vip;


class Vip {
	
	public function updatePaymentStatus($uid,$nonce,$orderid,$money) {
		$rs = array();

		$model = new Model_Vip();
		
		$rs = $model->updatePaymentStatus($uid,$nonce,$orderid,$money);

		return $rs;
	}

    /* VIP */
	public function getVip() {

        $key='vip';
		$rules=\App\getcaches($key);
		if(!$rules){
            $model = new Model_Vip();
			$rules= $model->getVip();
            if($rules){
                \App\setcaches($key,$rules);
            }
			
		}
		
		$lang=\PhalApi\DI()->lang;
		foreach($rules as $k=>$v){
			if($lang=='en'){
				$v['name']=$v['name_en'];
			}
			$rules[$k]=$v;
		}
		

		return $rules;
	}

    /* 我的VIP */
	public function myVip($uid) {
        
        $rs=[
            'isvip'=>'0',
            'endtime'=>'',
        ];

        $nowtime=time();
        
        $model = new Model_Vip();
        $info= $model->myVip($uid);
        if($info){
            if($info['endtime'] > $nowtime){
                $rs['isvip']='1';
                $rs['endtime']=date('Y-m-d',$info['endtime']);
            }
        }
		return $rs;
	}

    /* 钻石购买VIP */
	public function buyVip($uid,$vipid) {   
        $rs = array('code' => 0, 'msg' => \PhalApi\T('支付成功'), 'info' => array());
        
        $vipinfo=[];
        
        $list=$this->getVip();
        
        foreach($list as $k=>$v){
            if($v['id']==$vipid){
                $vipinfo=$v;
                break;
            }
        }
        
        if(!$vipinfo){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('信息错误');
			return $rs;
        }
        
        
        $total=$vipinfo['coin'];
        if($total<=0){
            $rs['code']=1004;
			$rs['msg']=\PhalApi\T('信息错误');
			return $rs;
        }
        
        $addtime=time();
		$type='0';
		$action='6';
        
        /* 更新用户余额 消费 */
		$ifok =\App\upCoin($uid,$total);
        if(!$ifok){
            $rs['code'] = 1003;
			$rs['msg'] = \PhalApi\T('余额不足');
			return $rs;
        }
        
        $insert=array(
            "type"=>$type,
            "action"=>$action,
            "uid"=>$uid,
            "touid"=>$uid,
            "actionid"=>$vipid,
            "nums"=>1,
            "totalcoin"=>$total,
            "addtime"=>$addtime 
        );
        
        \App\addCoinRecord($insert);
         
        $model = new Model_Vip();
        
        $info= $model->myVip($uid);
        
        if(!$info){
            /* 未购买过 */
            $data=[
                'uid'=>$uid,
                'addtime'=>$addtime,
                'endtime'=>$addtime + $vipinfo['length'],
            ];
            $model->setVip($data);
        }else if($info['endtime']< $addtime){
            /* 购买-已过期 */
            $data=[
                'addtime'=>$addtime,
                'endtime'=>$addtime + $vipinfo['length'],
            ];
            $model->upVip($uid,$data);
        }else{
            /* 续费 */
            $data=[
                'addtime'=>$addtime,
                'endtime'=>$info['endtime'] + $vipinfo['length'],
            ];
            $model->upVip($uid,$data);
        }
        
        $result=[
            'isvip'=>'1',
            'endtime'=>date('Y-m-d',$data['endtime']),
        ];

        $rs['info'][0]=$result;
        
		return $rs;
	}

    /* 生成订单 */
	public function setOrder($vipid,$orderinfo) {
		$rs = array('code' => 0, 'msg' => '', 'info' => array());
        
        
        $viplist=$this->getVip();
        $vipinfo=[];
        
        foreach($viplist as $k=>$v){
            if($v['id']==$vipid){
                $vipinfo=$v;
                break;
            }
        }
        
        if(!$vipinfo){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('信息错误');
			return $rs;	
        }
        
        if($vipinfo['coin_money']!=$orderinfo['money'] ){
			$rs['code']=1004;
			$rs['msg']=\PhalApi\T('信息错误');
			return $rs;
		}
		
		$orderinfo['length']=$vipinfo['length'];
		$orderinfo['coin']=$vipinfo['coin'];
        
        $model = new Model_Vip();
		$result = $model->setOrder($orderinfo);
        
        if(!$result){
            $rs['code']=1005;
			$rs['msg']=\PhalApi\T('订单创建失败，请重试');
			return $rs;
        }

		return $rs;
	}
	
}
